require(['jquery', 'cookie', 'app', 'lay'], function ($) {
    $("#qx").load("./vsp?code=1555508458593");
    $("#sj").load("./vsp?code=1555508458594");
    layui.use(['element', 'layer'], function () {
        var layer = layui.layer;
        layer.load();
        var element = layui.element;
        element.on('tab(docDemoTabBrief)', function(data){
            switch (data.index) {
                case 0:qxClose();qxKai($("#qx"));break;
                case 1:qxClose();qxKai($("#sj"));break;
            }
            
        });
    });
    require(['vue'], function (Vue) {
        var role = new Vue({
            el: "#role",
            data: {
                list: "",
                roleCode: "",
                roleName: "",
                num: "",
                radomCode: "",
                day: "",
                navig_list: "",
                navUUID: "",
                navig: "",
                th: {
                    uuid: "",
                    roleCode: "",
                    roleName: "",
                    roleNum: "",
                    methodTime: ""
                }
            },
            created: function () {
                this.loadPage(0, 10);
                this.day = getDete("ymd");
                setTimeout(function () {
                    layui.use('element', function () {
                        var element = layui.element;
                        layer.closeAll('loading');
                    });
                },200);
                //总数
                //var sumNum=playAjax("1555508458589","");
                //pageYer(sumNum,1,"test1",this.loadPage);
            },
            methods: {
                //设置数值
                loadPage: function (begin, end) {
                    var jon = {};
                    jon['begin'] = begin;
                    jon['end'] = end;
                    //jon['navig'] = this.navig;
                    if (this.roleCode != null && this.roleCode != '') {
                        jon['roleCode'] = this.roleCode;
                    } else {
                        jon['roleCode'] = "";
                    }

                    if (this.roleName != null && this.roleName != '') {
                        jon['roleName'] = this.roleName;
                    } else {
                        jon['roleName'] = "";
                    }
                    this.list = JSON.parse(playAjax("1555508458590", JSON.stringify(jon)))['data']['data'];
                    this.num = JSON.parse(playAjax("1555508458590", JSON.stringify(jon)))['data']['num'];

                },
                loadPageNAV: function () {
                    var jon = {};
                    //jon['navig'] = this.navig;
                    if (this.navUUID != null && this.navUUID != '') {
                        jon['uuid'] = this.navUUID;
                    } else {
                        jon['uuid'] = "";
                    }

                    if (this.navig != null && this.navig != '') {
                        jon['navig'] = this.navig;
                    } else {
                        jon['navig'] = "";
                    }

                    if (this.th.roleCode != null && this.th.roleCode!= '') {
                        jon['roleCode'] = this.th.roleCode;
                    } else {
                        jon['roleCode'] = "";
                    }
                    debugger;
                    this.navig_list = JSON.parse(playAjax("1555508458591", JSON.stringify(jon)))['data'];
                    //this.num = JSON.parse(playAjax("1555508458591", JSON.stringify(jon)))['data']['num'];
                    setTimeout(function () {
                        referFrom();
                    }, 0)
                },
                referXadmin: function () {
                    window.location.reload();
                },
                pre_home: function () {
                    $("#list").show();
                    $("#seach").show();
                    $("#add").hide();
                    $("#sq").hide();
                    $("#update").hide();
                    $("#sq_xq").hide();
                    $("#xq").hide();
                },
                show_add: function () {
                    $("#list").hide();
                    $("#seach").hide();
                    $("#add").show();
                    this.radomCode = "ROLE" + getCode(8);
                    this.loadPageNAV();
                },
                show_update: function () {
                    if (!this.checkSure()) {
                        return;
                    }

                    var td = $(".zh").find("td");
                    var xv = ($(td[0]).text()) - 1;
                    this.th = this.list[xv];
                    $("#list").hide();
                    $("#seach").hide();
                    $("#update").show();
                    //this.radomCode="ROLE"+getCode(8);
                    this.loadPageNAV();

                    $("#sq").show();

                },
                add_refer: function () {
                    this.navUUID = "";
                    this.navig = "";
                    this.loadPageNAV();
                },
                add_reset: function () {
                    this.radomCode = "ROLE" + getCode(8);
                    $('#roleName').html("");
                    this.add_refer();
                    $('#bz').html("");
                    $('tbody input').prop('checked', false);
                },
                up_reset: function () {
                    //this.radomCode="ROLE"+getCode(8);
                    $('#uproleName').html("");
                    $('#upbz').html("");
                    this.add_refer();
                    $('tbody input').prop('checked', false);
                },
                add_save: function () {
                    var jon = {};
                    jon = fromName($("#add_save"));
                    if (jon['roleName'] == "" || jon['roleName'] == null) {
                        alr("请双击输入角色名!");
                        return;
                    } else {
                        var re = JSON.parse(playAjax("1555508458592", JSON.stringify(jon)));
                        alr(re['title']);
                        setTimeout(function () {
                            role.referXadmin();
                        }, 500)
                    }
                },
                up_save: function () {
                    var jon = {};
                    jon = fromName($("#add_save"));
                    if (jon['roleName'] == "" || jon['roleName'] == null) {
                        alr("请双击输入角色名!");
                        return;
                    } else {
                        jon['uuid'] = this.th['uuid'];
                        var re = JSON.parse(playAjax("1555508458594", JSON.stringify(jon)));
                        alr(re['title']);
                        setTimeout(function () {
                            role.referXadmin();
                        }, 500)
                    }
                },
                up_sq: function () {
                    var ids=this.dboCheck();
                    if(ids.length==0){
                        alr("请至少选择一项进行授权!");
                        return;
                    }
                    var jon = {};
                    jon['roleCode'] = this.th['roleCode'];
                    jon['navigIds'] = ids.join(",");
                    var re=JSON.parse(playAjax("1555508458595",JSON.stringify(jon)));
                    alr(re['title']);
                    setTimeout(function () {
                        role.referXadmin();
                    },500)
                },
                editDBL: function (e) {
                    var e = e.target;
                    $(e).html("<input class='layui-input' onblur='editBlur(this)'/>");
                },
                editDBLTEXT: function (e) {
                    var e = e.target;
                    $(e).html("<textarea class='layui-textarea' onblur='editBlur(this)'></textarea>");
                },
                deleted: function () {
                    if (!this.checkSure()) {
                        return;
                    }
                    layer.open({
                        content: '确定要删除吗？'
                        , btn: ['确认', '取消']
                        , yes: function (index, layero) {
                            var jon = {};
                            jon['uuid'] = role.checkSure();
                            var re = JSON.parse(playAjax("1555508458593", JSON.stringify(jon)));
                            alr(re['title']);
                            //按钮【按钮一】的回调
                            layer.closeAll();
                            role.referXadmin();
                        }
                        , btn2: function (index, layero) {
                            //按钮【按钮二】的回调
                            layer.closeAll();
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                        , cancel: function () {
                            //右上角关闭回调
                            layer.closeAll();
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    });
                },
                checkSure: function () {
                    if ($(".zh").length <= 0) {
                        alr("未选中！请选择一条记录");
                        return false;
                    } else {
                        var td = $(".zh").find("td");
                        var uuid = $(td[1]).text();
                        return uuid;
                    }
                },
                dboCheck: function () {
                    var ids = [];
                    // 获取选中的id
                    $('#sq tbody input').each(function (index, el) {
                        if ($(this).prop('checked')) {
                            ids.push($(this).val())
                        }
                    });
                    return ids;
                },
                show_xq:function () {
                    if (!this.checkSure()) {
                        return;
                    }
                    var td = $(".zh").find("td");
                    var xv = ($(td[0]).text()) - 1;
                    this.th = this.list[xv];
                    $("#list").hide();
                    $("#seach").hide();
                    $("#xq").show();
                    //this.radomCode = "ROLE" + getCode(8);
                    this.loadPageNAV();
                    $("#sq_xq").show();
                }
            },
        })
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            var jon = {};
            //执行一个laypage实例
            laypage.render({
                elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
                , count: role.num//数据总数，从服务端得到
                , limit: 10
                , jump: function (obj, first) {
                    //obj包含了当前分页的所有参数，比如：
                    var x = obj.curr; //得到当前页，以便向服务端请求对应页的数据。
                    var y = obj.limit; //得到每页显示的条数
                    //首次不执行
                    if (!first) {
                        //do something
                        var begin = (x - 1) * y;
                        var end = y;
                        role.loadPage(begin, end);
                    }
                }
            });
        });
        layui.use('form', function () {
            var form = layui.form;
            form.render();
            form.on('submit(soso)', function (data) {
                role.roleCode = data.field.roleCode;
                role.roleName = data.field.roleName;
                role.loadPage(0, 10);
                return false;
            });
            form.on('submit(sosoNAV)', function (data) {
                role.navUUID = data.field.uuid;
                role.navig = data.field.navig;
                role.loadPageNAV();
                return false;
            });

            form.on('checkbox(checkall)', function (data) {
                if (data.elem.checked) {
                    $('tbody input').prop('checked', true);
                } else {
                    $('tbody input').prop('checked', false);
                }
                form.render('checkbox');
            });
        });
    });

});
function qxClose() {
    $("#qx").hide();
    $("#sj").hide();
}
function qxKai(e) {
    $(e).show();
}